// pages/addGood/goodsCate/goodsCate.js
import {
  systematicsList,
  getProductBrand,
  brandLike,
  seriesLike
} from '../../../api.js'
let app = getApp();
Page({
  data: {
    lis_type: 3,
    lis: [],
    lis_text: {
      lis_three: {
        value: '',
        id: ''
      },
      lis_four: {
        value: '',
        id: ''
      },
    },
    btn_right: true,
    search_input:'',
    alert_order_hid:true,
    brand_other:'',
    systematics_other:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var jsonStr = options.lis
    jsonStr = jsonStr.replace(/\ufeff/g, "");
    var jj = JSON.parse(jsonStr);
    console.log(jj)
    var that = this
    wx.showLoading({
      title: '正在加载...',
    })
    if (jj.brand_title != '') {
      that.setData({
        btn_right: false
      })
    }
    that.data.lis_text.lis_three.value = jj.brand_title
    that.data.lis_text.lis_three.id = jj.brand_id
    that.data.lis_text.lis_four.value = jj.systematics_title
    that.data.lis_text.lis_four.id = jj.systematics_id
    that.setData({
      lis_text: that.data.lis_text,
      brand_other: jj.brand_other,
      systematics_other: jj.systematics_other
    })
    that.up_lis()
  },
  lis_btn: function (e) {
    wx.showLoading({
      title: '正在加载...',
    })
    var id = e.currentTarget.dataset.id
    var ind = e.currentTarget.dataset.ind
    var that = this
    if (that.data.lis_type == 3 && id == 171) {
      wx.hideLoading()
      that.setData({
        alert_order_hid:false
      })
    }else{
      if (that.data.lis_type == 3) {
        that.data.lis_text.lis_three.value = that.data.lis[ind].title_all
        that.data.lis_text.lis_three.id = that.data.lis[ind].id
        that.setData({
          lis_text: that.data.lis_text,
          lis_type: 4,
          search_input: ''
        })
        that.up_lis(id, 4)
        return
      }
      if (that.data.lis_type == 4) {
        that.data.lis_text.lis_four.value = that.data.lis[ind].title
        that.data.lis_text.lis_four.id = that.data.lis[ind].id
        that.setData({
          lis_text: that.data.lis_text,
          btn_right: false,
          search_input: ''
        })
        wx.hideLoading()
        return
      }
    }
  },
  up_lis(lis_id, lis_type) {
     if (lis_type == 4) {
      getProductBrand({
        token: getApp().globalData.token,
        brand_id: lis_id,
        // type: lis_type
      }).then(res => {
        if (res.data.code == 666) {
          this.setData({
            lis: res.data.data
          })
          if (res.data.data.length == 0) {
            this.setData({
              btn_right: false
            })
            wx.hideLoading()
          }
          wx.hideLoading()
        } else if (res.data.code == 111111) {
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/user/login/login',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
    } else {
      console.log(1111)
      getProductBrand({
        token: getApp().globalData.token,
        // brand_id: lis_id,
        // type: lis_type
      }).then(res => {
        if (res.data.code == 666) {
          console.log(res.data.data)
          this.setData({
            lis: res.data.data,
          })
          if (res.data.data.length == 0) {
            this.setData({
              btn_right: false
            })
            wx.hideLoading()
          }
          wx.hideLoading()
        } else if (res.data.code == 111111) {
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/user/login/login',
            })
          }, 1500)
        }
      })
    }
  },

  btn_left(e) {
    wx.showLoading({
      title: '正在加载...',
    })
    this.setData({
      lis: []
    })
    var id = e.currentTarget.dataset.id
    var typ = e.currentTarget.dataset.typ
    var that = this
    this.up_lis(id, typ)
    if (typ == 3) {
        that.data.lis_text.lis_three.value = '',
        that.data.lis_text.lis_three.id = '',
        that.data.lis_text.lis_four.value = '',
        that.setData({
          btn_right: true,
          lis_text: that.data.lis_text,
          lis_type: 3
        })
    }
    if (typ == 4) {
      that.data.lis_text.lis_four.value = '',
        that.setData({
          btn_right: true,
          lis_text: that.data.lis_text,
          lis_type: 4
        })
    }
  },
  queren_btn(e) {
    console.log(this.data.lis_text)
    var lis_text = this.data.lis_text
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    var that = this
    prevPage.setData({
      'addEdit.brand_title': lis_text.lis_three.value,
      'addEdit.systematics_title': lis_text.lis_four.value,
      'addEdit.brand_id': lis_text.lis_three.id,
      'addEdit.systematics_id': lis_text.lis_four.id,
      'addEdit.brand_other': that.data.brand_other,
      'addEdit.systematics_other': that.data.systematics_other
    })
    if (prevPage.data.fuc == 0) {
      prevPage.productSn()
    }
    wx.navigateBack({
      delta: 1
    })
  },
  // 模糊搜索
  brandLike(e) {
    var that = this;
    var text = e.detail.value
    if (that.data.lis_type == 3) {
      brandLike({
        token: getApp().globalData.token,
        title_all: text
      }).then(res => {
        that.setData({
          lis: res.data.data,          
        })
      })
    } else if (that.data.lis_type == 4) {
      seriesLike({
        token: getApp().globalData.token,
        title: text,
        brand_id: that.data.lis_text.lis_three.id
      }).then(res => {
        that.setData({
          lis: res.data.data
        })
      })
    }
  },
  formReset(){
    this.setData({
      alert_order_hid:true
    })
  },
  formSubmit(e){
    console.log(e.detail.value.brand_other)
    var brand_other = e.detail.value.brand_other
    var that = this
    if (!brand_other){
      wx.showToast({
        title: '请填写商品品牌',
        icon:'none'
      })
    }else{
      that.data.lis_text.lis_three.value = 'Other/自定义'
      that.data.lis_text.lis_three.id = 171
      that.setData({
        brand_other: e.detail.value.brand_other,
        systematics_other: e.detail.value.systematics_other,
        lis_text: that.data.lis_text,
        btn_right: false,
        search_input: '',
        alert_order_hid:true
      })
    }
  }
})